package Views;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import BusinessObjects.ServerInfo;
import BusinessObjects.ServerMessage;
import Models.ServerConnexionModel;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class ServerConnexionView extends JFrame implements Observer {
	private JLabel alert;
	private ServerConnexionModel model;
	private JTextField textFieldIP;
	private JTextField textFieldPortServer;
	private JComboBox<String> comboBoxProtocole;
	
	public ServerConnexionView(ServerConnexionModel model, ActionListener controller, String name) {
		this.model = model;
		model.addObserver(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 350);
		JPanel contentPane = new JPanel();
		contentPane.setBackground(new Color(204, 204, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		contentPane.setLayout(null);
		setTitle(name);
		
		JButton buttonConnexion = new JButton("Connexion");
		buttonConnexion.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonConnexion.setBounds(170, 175, 109, 22);
		contentPane.add(buttonConnexion);	
		buttonConnexion.addActionListener(controller);
		buttonConnexion.setActionCommand("connexionToServer");
			
		setContentPane(contentPane);
		
		JLabel lblMotDePasse = new JLabel("Port serveur :");
		lblMotDePasse.setBounds(76, 150, 95, 14);
		contentPane.add(lblMotDePasse);
		
		JLabel lblNomDutilisateur = new JLabel("Protocole :");
		lblNomDutilisateur.setBounds(76, 91, 66, 14);
		contentPane.add(lblNomDutilisateur);
		
		JLabel lblLogin = new JLabel("Authentification au serveur");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLogin.setBounds(83, 36, 306, 22);
		contentPane.add(lblLogin);
		
		JLabel lblAdresseIp = new JLabel("Adresse :");
		lblAdresseIp.setBounds(76, 119, 66, 14);
		contentPane.add(lblAdresseIp);
		
		textFieldIP = new JTextField();
		textFieldIP.setText("localhost");
		textFieldIP.setBounds(170, 116, 109, 20);
		contentPane.add(textFieldIP);
		textFieldIP.setColumns(10);
		
		textFieldPortServer = new JTextField();
		textFieldPortServer.setBounds(170, 147, 58, 20);
		contentPane.add(textFieldPortServer);
		textFieldPortServer.setColumns(4);
		
		comboBoxProtocole = new JComboBox<String>();
		comboBoxProtocole.setModel(new DefaultComboBoxModel<String>(new String[] {"TCP/IP", "UDP"}));
		comboBoxProtocole.setBounds(170, 88, 109, 20);
		contentPane.add(comboBoxProtocole);
		
		alert = new JLabel("");
		alert.setForeground(Color.RED);
		alert.setBounds(36, 221, 338, 29);
		contentPane.add(alert);		
		setVisible(true);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		alert.setText(model.getAlert());
	}
	
	public ServerInfo getServerInfos() {
		return new ServerInfo(0, (String) comboBoxProtocole.getSelectedItem(),
				textFieldIP.getText(), textFieldPortServer.getText());
	}
}
